package com.os.exception;

import com.os.result.Resp;
import com.os.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * 全局异常处理
 * @author 风亦未止
 * @date 2023/5/2 17:53
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = IOException.class)
    public Resp handleDataAccessException(IOException e){
        log.error(e.getMessage());
        return Resp.failed(ResultCode.FAILED);
    }

    @ResponseBody
    @ExceptionHandler(value = ApiException.class)
    public Resp handle(ApiException e) {
        if (e.getErrorCode() != null) {
            return Resp.failed(e.getErrorCode());
        }
        return Resp.failed(e.getMessage());
    }
}
